/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AIproccess;

import Army.BuildingAndUnit;
import Army.ComputerTeam;
import Army.unit.Unit;
import GameObject.LayerManager;
import Army.task.TaskGuard;
import java.util.Random;

/**
 *
 * @author Huy
 */
public abstract class AIManager {

	public static Random RANDOM = new Random(System.currentTimeMillis());
	protected static int WAGES = 10;
	//private PlayerTeam[] listTeam;
	protected LayerManager layer;
	protected PlayerAIActack[] attackers;
	protected PlayerAIDevelope[] developers;
	protected int wages = 100;
	protected long time = 0;
	protected boolean isPlay = true;

	public AIManager() {
		
	}
	public AIManager(LayerManager lm) {
		layer = lm;
		int n = layer.getListTeam().length;
		developers = new PlayerAIDevelope[n - 1];
		attackers = new PlayerAIActack[n - 1];
		int k = 0;
		for (int i = 0; i < n; i++) {
			if (!layer.getListTeam()[i].isIsControled()) {
				developers[k] = new PlayerAIDevelopeNoob(layer, (ComputerTeam) layer.getListTeam()[i]);
				attackers[k] = new PlayerAIActackNoob(layer, (ComputerTeam) layer.getListTeam()[i]);
				k++;
			}
		}


	}
	protected Thread killTaskThread = new Thread() {
		@Override
		public void run() {
			while (isPlay) {
				proccess();
				try {
					sleep((int) (100));
				} catch (InterruptedException e) {
				}
			}
		}
	};

	public void start() {
		isPlay = true;
		killTaskThread.start();
		for (int i = 0; i < attackers.length; i++) {
			
			attackers[i].setIsPlay(true);
			attackers[i].start();
			developers[i].setIsPlay(true);
			developers[i].start();
		}
	}

	public void setIsPause(boolean isPause)
	{
		for (int i = 0; i < attackers.length; i++) {
			attackers[i].setIsPause(isPause);
			developers[i].setIsPause(isPause);
		}
	}
	public void setDevelopeIsPause(boolean isPause)
	{
		for (int i = 0; i < attackers.length; i++) {
			developers[i].setIsPause(isPause);
		}
	}
	public void setAttackIsPause(boolean isPause)
	{
		for (int i = 0; i < attackers.length; i++) {
			attackers[i].setIsPause(isPause);
		}
	}
	public void stop() {
		isPlay = false;
		for (int i = 0; i < attackers.length; i++) {
			attackers[i].setIsPlay(false);
			developers[i].setIsPlay(false);
		}
	}

	public void proccess() {
		for (int i = 0; i < layer.getListTeam().length; i++) {
			for (int j = 0; j < layer.getListTeam()[i].getListUnit().size(); j++) {
				Unit unit = layer.getListTeam()[i].getListUnit().get(j);
				setTask(unit, i);
			}
		}
	}

	public void setTask(BuildingAndUnit unit, int team) {

		if (unit.getType() == BuildingAndUnit.Type.UnitWorker) {
			return;
		}
		double min = -1;
		Unit target = null;
		for (int i = 0; i < layer.getListTeam().length; i++) {
			if (i == team) {
				continue;
			}
			for (int j = 0; j < layer.getListTeam()[i].getListUnit().size(); j++) {
				Unit unit2 = layer.getListTeam()[i].getListUnit().get(j);
				if (unit2 == null) {
					continue;
				}
				double d = unit.getPosition().distance(unit2.getPosition());
				if (d < unit.getViewRange() && (min == -1 || min > d)) {
					target = unit2;
					min = d;
				}
			}
		}
		if (target != null
				&& (unit.getTask() == null || unit.getTask().isInView(target.getPosition()))) {
			unit.setTask(new TaskGuard(unit, target));
		}
	}
}
